package com.itheima.utildemo;

import javax.swing.*;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Demo03 {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(8);
        for (int i = 0; i < 8; i++) {
            Player player = new Player(cyclicBarrier);
            Thread t = new Thread(player);
            t.setName("运动员00"+i);
            t.start();
        }


    }
}
/*
class  Judgment implements Runnable{
    @Override
    public void run() {
        //裁判员打枪
    }
}
*/

class  Player implements Runnable{
    private CyclicBarrier cyclicBarrier;

    public Player(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        //先就位
        String name = Thread.currentThread().getName();
        System.out.println(name+"已经就位");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        //等一声抢响
        System.out.println(name+"出发了");
        //跑
        Random r = new Random();
        try {
            int time = r.nextInt(400) + 9580;
            Thread.sleep(time);
            System.out.println(name+"跑了"+time+"到达了终点");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
